﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.Services;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime;
using Galaktika.BI.Extensibility.Dom.Dac;
using Ranet.AgOlap.Controls.PivotGrid.ClientServer;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.DataAccess;
using Ranet.Olap.Core.Data;
using Ranet.Olap.Core;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands
{
    public class ExecuteQueryCommand : ServerCommand
    {
        public ExecuteQueryCommand(InvokeSchema invoker)
            : base(Galaktika.BI.Silverlight.Commands.CommandId.ExecuteQueryCommandId)
        {
            this.ContextId = invoker[KnownInvokeArgs.ContextId];
            this.DataSourceId = invoker[KnownInvokeArgs.DataSourceId];
            this.ConnectionId = invoker[KnownInvokeArgs.ConnectionId];
            this.Args = invoker[KnownInvokeArgs.ExecuteQueryCommand];
        }

        public string ContextId { get; private set; }
        public string ConnectionId { get; private set; }
        public string DataSourceId { get; private set; }
        public string Args { get; private set; }

        private IBindingContext Context
        {
            get
            {
                return DataBinder.Current.GetContext(this.ContextId);
            }
        }

        OlapDataManager m_DataManager = null;
        private OlapDataManager DataManager
        {
            get
            {
                if (m_DataManager == null)
                {
                    if (!String.IsNullOrEmpty(this.DataSourceId))
                    {
                        IBindingManager bindingMgr = DataBinder.Current.GetBindingManager(this.ContextId, this.DataSourceId);
                        if (bindingMgr != null)
                        {
                            m_DataManager = bindingMgr.DataManager as OlapDataManager;
                        }
                    }
                    else
                    {
                        BrickConnection conn = DataBinder.Current.GetNamedObject(this.ConnectionId) as BrickConnection;
                        if (conn != null)
                        {
                            m_DataManager = new OlapDataManager(new ConnectionInfo(conn.Name, conn.ConnectionString), String.Empty, String.Empty);
                        }
                    }
                }

                return m_DataManager;
            }
        }

        public override Galaktika.BI.Runtime.Services.InvokeResultSchema Invoke()
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            res.ContentType = KnownContentTypes.MultidimData;

            MdxQueryArgs args = XmlUtility.XmlStr2Obj<MdxQueryArgs>(this.Args);
            if (DataManager != null && args != null)
            {
                // Заглушка - т.к. несколько раз была ситуация когда DataManager.Context оставался null
                if (DataManager.Context == null)
                    DataManager.Context = this.Context;

                if (args.Type == QueryExecutingType.Query)
                {
                    CellSetData csd = DataManager.ExecuteQuery(args.Query);
                    res.Content = XmlUtility.Obj2XmlStr(csd, Common.Namespace);
                }
                if (args.Type == QueryExecutingType.NonQuery)
                {
                    int rez = DataManager.ExecuteNonQuery(args.Query);
                    res.Content = XmlUtility.Obj2XmlStr(rez, Common.Namespace);
                }
            }

            return res;
        }
    }
}
